package org.vacoor.ticket.demo;

import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import org.vacoor.ticket.Constant;
import org.vacoor.ticket.bean.Passenger;
import org.vacoor.ticket.bean.SeatType;
import org.vacoor.ticket.bean.Station;
import org.vacoor.ticket.bean.TicketDto;
import org.vacoor.ticket.bean.TimeRegionType;
import org.vacoor.ticket.bean.TrainPassType;
import org.vacoor.ticket.bean.TrainType;
import org.vacoor.ticket.bean.option.QueueInfo;
import org.vacoor.ticket.bean.param.impl.OrderParam;
import org.vacoor.ticket.bean.param.impl.QueueParam;
import org.vacoor.ticket.bean.param.impl.TicketQueryParam;
import org.vacoor.ticket.executor.defaults.DefaultExecutor;
import org.vacoor.ticket.handler.impl.option.QueueResultHandler;
import org.vacoor.ticket.service.TicketService;
import org.vacoor.ticket.util.DateUtils;
/**
 * 这是一个简单的测试类,体现12306购票流程:
 * 1.初始化输入信息,获取城市代码等操作<br>
 * 2.如果存在车次,获取相应列车编号<br>
 * 3.登录(也可以放在最前面)<br>
 * 4.查询车票获取订单,使用上面得到的查询信息<br>
 * 5.订单添加乘客信息<br>
 * 6.检测订单(需提交订单信息,提交订单前必须执行)<br>
 * 7.获取队列信息(前面多少人,余票信息,可省略,可从获取订单后任意时刻获取)<br>
 * 8.提交订单(参数和检测订单完全一样,包括验证码)<br>
 * 9.获取等待时间(这一步一旦执行则之前订单信息全部无效,必须重新获取订单检测提交)<br>
 * 10.查询未完成订单,确定订单状态(这步请在提交订单后,停留几秒,等服务器处理后执行)<br>
 * @author vacoor
 *
 */

public class BaseDemo {
	public static void main(String[] args) throws Exception {
		JOptionPane.showMessageDialog(null, "本软件仅供娱乐,请在终端运行,以便获取相应信息\r\n如有Bug,请发送详情到:Vacoor@QQ.com");
		String location = System.getProperty("configLocation",System.getProperty("user.dir")+ "/" + "config.txt");
		Configuration conf = new Configuration();
		conf.configure(location); //这里可以修改以下
		boolean showInfo = conf.isShowConfirm();
		String username = conf.getUsername();
		String passwd = conf.getPassword();
		Date date = conf.getDate();
		Station from = conf.getFrom();
		Station arrive = conf.getArrive();
		//
		// 车次
		String trainNo = conf.getTrainNo();
		boolean precision = conf.isPrecision();
		// 查询不到结果时候,循环查询次数
		long queryCount = conf.getQueryCount();
		// 时间段 00:00--24:00
		TimeRegionType region = conf.getRegion();
		// 列车类型
		TrainType[] trainType = conf.getTrainType();
		// 列车经过类型
		TrainPassType passType = conf.getPassType();
		List<Passenger> ps = conf.getPassengers();

		//最多可以买5张
		if(ps.size() > 5){
			
		}
		//----------start-----------
		if(showInfo) {
			StringBuilder msg = new StringBuilder();
			msg.append("信息如下请核实后点击确定:\r\n" +
						"登录设置:\r\n---------\r\n" +
						"用户名:" + username + "\r\n" +
						"密  码:" + passwd + "\r\n" + 
						"查询设置:\r\n---------\r\n" +
						"无票循环查询次数:" + queryCount + "\r\n" +
						"日期:" + DateUtils.formatDate(date, "yyyy-MM-dd") + "\r\n" +
						"出发站:" + from.getName() + "\r\n" +
						"目的站:" + arrive.getName() + "\r\n" +
						"车次:" + trainNo + "\r\n" + 
						"精确匹配站点名称:" + precision + "\r\n" + 
						"时间段:" + region + "\r\n" + 
						"列车类型:"
						);
			for(TrainType t:trainType) {
				msg.append(t.getName() + " ");
			}
			msg.append("\r\n乘客信息\r\n---------\r\n");
			for(int i=0;i<ps.size();i++) {
				Passenger p = ps.get(i);
				msg.append(
					"乘客"+ (i+1) + "\r\n" + 
					"姓名:" + p.getName() + ", " +
					"座位类型:" + p.getSeatType().getName() + ", " +
					"车票类型:" + p.getTicketType().getName() + ", " +
					"证件类型:" + p.getCardType().getName() + ", " +
					"证件号码:" + p.getCardNo() + "\r\n "
						);
			}
			
			
			int flag = JOptionPane.showConfirmDialog(null,msg
					);
			if(flag != 0) {
				System.out.println("你选择了取消");
				System.exit(0);
			}
		}
		System.out.println("Start...");
		System.out.println("等待服务器响应...");
		//现在不支持不同乘客不同座位类型
		SeatType seatType = ps.get(0).getSeatType(); 

		// -------------------------------
		// 是否是学生票查询
		//boolean isStuQuery = false;
		// ---------------------------------------------------------------------------------
		// 初始化查询
//		TicketQueryParam ticketQuery = TicketService.initQuery(isStuQuery, from, arrive, date,region, passType, trainType, trainCode);
		TicketQueryParam ticketQuery = TicketService.initQuery(from, arrive, date,region, passType, trainType, trainNo);
		// 登录
		TicketService.login(username, passwd, 10);
		// 查询余票
		List<TicketDto> tickets = TicketService.queryTickets(ticketQuery, seatType,ps.size(), precision,queryCount);
		//开始预订
		for (int m = 0; m < tickets.size(); m++) {
			TicketDto ticket = tickets.get(m);
			// 预订车票,获取订单
			OrderParam order = TicketService.bookingTicket(ticketQuery, ticket);
			//添加乘客信息
			order.setPassengers(ps);
			//检测订单,这步必须进行
			TicketService.checkOrder(order);
			System.out.println("服务器校验成功....");
			// 12306会在这里获取队列信息
			System.out.println("获取车票队列信息....");
			QueueParam qq = new QueueParam(order.getTrainDate(),order.getTrainId(), order.getTrainNo(), seatType,order.getFromStation(), order.getArriveStation(), order.getRemainTicketsDetail());
			QueueInfo queue = DefaultExecutor.execute(Constant.ORDER_QUENE_URL, qq,"GET", new QueueResultHandler());
			if(!queue.isBuy()) {
				System.out.println("排队人数已经超过余票张数,不能提交.");
				continue;
			}
			System.out.println("前面还有" + queue.getQueueCount() + "人");
			// 提交订单
			String msg = TicketService.submitOrder(order);
			if(!"Y".equals(msg)) {
				System.out.println("提交订单失败:" + msg);
				System.out.println("尝试下一个车次");
				continue;
			}
			System.out.println("提交订单成功");
			//检测订单是否成功
			boolean isSuccess = TicketService.checkOrderState();
			System.out.println("订单" + (isSuccess ? "成功" : "失败"));
			if(!isSuccess) {
				System.out.println("尝试下一个车次");
			}else {
				System.exit(0);
			}
		}
		System.out.println("所有符合条件车次都以经预订过,没有预订成功,很抱歉");
	}
}
